local skel = fk.CreateSkill {
  name = "emo__danxin",
  tags = {Skill.Permanent},
}

Fk:loadTranslationTable{
  ["emo__danxin"] = "丹心",
  [":emo__danxin"] = "持恒技，每当你受到伤害后，你可以观看一名其他角色的手牌并选一项：1.获得其所有【杀】，令其摸等量张牌；2.对其发动〖讨逆〗。",

  ["#emo__danxin-choose"] = "丹心：你可观看一名其他角色的手牌，选择获得其的杀或对其“讨逆”",
  ["#emo__danxin-watch"] = "丹心：请观看 %dest 的手牌中(其有 %arg 张【杀】)",
  ["#emo__danxin_get"] = "获得其中【杀】",
  ["#emo__danxin_taoni"] = "对其发动“讨逆”",
}

skel:addEffect(fk.Damaged, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and table.find(player.room.alive_players, function (p)
      return p ~= player and not p:isKongcheng()
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local targets = table.filter(player.room:getOtherPlayers(player, false), function (p) return not p:isKongcheng() end)
    local tos = player.room:askToChoosePlayers(player, {
      targets = targets, max_num = 1, min_num = 1, skill_name = skel.name, cancelable = true,
      prompt = "#emo__danxin-choose",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    if to:isKongcheng() then return end
    local hand = to:getCardIds("h")
    local cards = table.filter(hand, function (id) return Fk:getCardById(id).trueName == "slash" end)
    local _, choice = room:askToChooseCardsAndChoice(player, {
      cards = cards, all_cards = hand, skill_name = skel.name, min_num = 0, max_num = 0,
      choices = #cards > 0 and {"#emo__danxin_get", "#emo__danxin_taoni"} or {},
      cancel_choices = #cards == 0 and {"Cancel"} or {},
      prompt = "#emo__danxin-watch::"..to.id..":"..#cards,
    })
    if choice == "#emo__danxin_get" then
      room:obtainCard(player, cards, false, fk.ReasonPrey, player, skel.name)
      if not to.dead then
        to:drawCards(#cards, skel.name)
      end
    elseif choice == "#emo__danxin_taoni" then
      local taoni = Fk.skills["emo__taoni"]
      if taoni and taoni:isInstanceOf(ActiveSkill) then
        player:addSkillUseHistory(taoni.name, 1) -- 增加发动次数，为了正确计数
        ---@cast taoni ActiveSkill
        taoni:onUse(room, SkillUseData:new {from = player, tos = {to}, cards = {}})
      end
    end
  end,
})

return skel
